﻿//Copyright (c) Microsoft Corporation
//
//All rights reserved. 
//
//Licensed under the Apache License, Version 2.0 (the ""License""); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. 
//
//See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.

using System;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using TED.WindowsAzure.Unplugged.Mobile;
using TED.WindowsAzure.Unplugged.Mobile.Interfaces;
using TED.WindowsAzure.Unplugged.Mobile.RemoteStorage.AzureBlob.Storage;
using TED.WindowsAzure.Unplugged.Mobile.Sync.Folder;
using System.Collections.Generic;
using System.Collections.ObjectModel;

// The Blank Application template is documented at http://go.microsoft.com/fwlink/?LinkId=234227

namespace DPE.TED.OCAF.Win8SampleClient
{
    /// <summary>
    /// Provides application-specific behavior to supplement the default Application class.
    /// </summary>
    sealed partial class App : Application
    {
        public static IUnpluggedMobileServiceClient MobileClient = new UnpluggedMobileServiceClient( new Uri( "[REPLACE with Mobile Services URL]" ), "[REPLACE with Mobile Services Key]" );
        public static ISyncFolder DocumentsFolder;
        public static Dictionary<string, ISyncFolder> CommentsFolders;

           
        /// <summary>
        /// Initializes the singleton application object.  This is the first line of authored code
        /// executed, and as such is the logical equivalent of main() or WinMain().
        /// </summary>
        public App()
        {
            this.InitializeComponent();
            this.Suspending += OnSuspending;
        }

        /// <summary>
        /// Change the current folder viewed on the MainPage
        /// </summary>
        /// <param name="folder"></param>
        public static async void SwitchFolder( string folder )
        {
            App.DocumentsFolder = await App.MobileClient.SyncContext.GetSyncFolderAsync( folder );
        }


        /// <summary>
        /// Invoked when the application is launched normally by the end user.  Other entry points
        /// will be used such as when the application is launched to open a specific file.
        /// </summary>
        /// <param name="e">Details about the launch request and process.</param>
        protected async override void OnLaunched(LaunchActivatedEventArgs e)
        {

#if DEBUG
            if (System.Diagnostics.Debugger.IsAttached)
            {
                this.DebugSettings.EnableFrameRateCounter = true;
            }
#endif

            Frame rootFrame = Window.Current.Content as Frame;

            await App.MobileClient.SyncContext.InitializeAsync(new WindowsAzureBlobStorage(MobileClient));
            App.DocumentsFolder = await App.MobileClient.SyncContext.GetSyncFolderAsync("documents");
            App.CommentsFolders = new Dictionary<string, ISyncFolder>();

            // Do not repeat app initialization when the Window already has content,
            // just ensure that the window is active
            if (rootFrame == null)
            {
                // Create a Frame to act as the navigation context and navigate to the first page
                rootFrame = new Frame();
                // Set the default language
                rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];

                // Place the frame in the current Window
                Window.Current.Content = rootFrame;
            }

            if (rootFrame.Content == null)
            {
                // When the navigation stack isn't restored navigate to the first page,
                // configuring the new page by passing required information as a navigation
                // parameter
                if (!rootFrame.Navigate(typeof(MainPage), e.Arguments))
                {
                    throw new Exception("Failed to create initial page");
                }
            }
            // Ensure the current window is active
            Window.Current.Activate();
            // http://go.microsoft.com/fwlink/?LinkId=290986&clcid=0x409
        }

        /// <summary>
        /// Invoked when application execution is being suspended.  Application state is saved
        /// without knowing whether the application will be terminated or resumed with the contents
        /// of memory still intact.
        /// </summary>
        /// <param name="sender">The source of the suspend request.</param>
        /// <param name="e">Details about the suspend request.</param>
        private void OnSuspending(object sender, SuspendingEventArgs e)
        {
            var deferral = e.SuspendingOperation.GetDeferral();
            deferral.Complete();
        }
    }
}
